package com.tanhua.server.service.impl;

import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.model.domain.Question;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.QuestionService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

@Service
public class QuestionServiceImpl implements QuestionService {

    @DubboReference
    private QuestionApi questionApi;
    //设置陌生人问题
    @Override
    public void setQuestion(String content) {
        //获取当前用户id
        Long userId = UserHolder.getUserId();
        //根据id查询当前用户的陌生人问题，注意：每个用户在question表中只会存在一条数据
        Question question = questionApi.queryQuestion(userId);
        //判断该对象是否为空，如果为空就证明该用户在表中并没有数据，不为空就证明已经存在了对应该用户ID的数据
        if (question==null){
            question = new Question();
            question.setUserId(userId);
            question.setTxt(content);
            questionApi.saveQuestion(question);
        }else {
            question.setTxt(content);
            questionApi.updateQuestion(question);
        }
    }
}
